//
//  JkmVCAnimatedTransitioning.swift
//  SinaWb
//
//  Created by Jiang on 5/16/16.
//  Copyright © 2016 Jiang. All rights reserved.
//

import UIKit

let  PresentationControlelrClickBackGroundButton = "PresentationControlelrClickBackGroundButton"
/**
    ### ModalPresent动画管理 ###
    #### 继承UIPresentationController ####
    1. 将presentedView添加到容器视图
    2. 控制容器视图的子视图的位置大小
    3. 添加一个UIControl监听背景的点击
    4. 控制presentedView的消失
 
    #### 继承UIViewControllerAnimatedTransitioning ####
    1. 展现动画的实现
    2. 消失动画的实现
 
    #### 代理UIViewControllerTransitioningDelegate ####
    1. 返回负责展现动画的UIViewControllerAnimatedTransitioning对象
    2. 返回负责消失动画的UIViewControllerAnimatedTransitioning对象
    3. 返回负责管理的UIPresentationController对象
 */
class JkmPresentationAniamtionManager:UIPresentationController{
    
    var isPresent  = true
    
    var viewFrame: CGRect?
    
    var backControl : UIControl = {
        let bac = UIControl(frame: UIScreen.mainScreen().bounds)
        bac.backgroundColor = UIColor.clearColor()
        
        return bac
    }()
    override func containerViewWillLayoutSubviews() {
        
        containerView?.addSubview(backControl)
        
        backControl.addTarget(self, action: #selector(ClickBackControl), forControlEvents: .TouchDown)
        
        containerView?.addSubview(presentedView()!)

        
        presentedView()!.frame = viewFrame!
        
    }
    
    @objc   private func  ClickBackControl(){
        
        presentedViewController.dismissViewControllerAnimated(true) {
            
            NSNotificationCenter.defaultCenter().postNotificationName(PresentationControlelrClickBackGroundButton, object: nil)
        }
    }

}

// MARK: - UIViewControllerTransitioningDelegate
extension JkmPresentationAniamtionManager:UIViewControllerTransitioningDelegate{

    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        isPresent = true
        return self
    }
    
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?{
        isPresent = true
        let  present = JkmPresentationAniamtionManager(presentedViewController: presented, presentingViewController: presenting)
        
        present.viewFrame = self.viewFrame
        
        return present
    }
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
}


// MARK: -  UIViewControllerAnimatedTransitioning
extension JkmPresentationAniamtionManager:UIViewControllerAnimatedTransitioning{
    /// 设置动画时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
        
        return 0.5
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
        if isPresent {
            //展现动画
            presentViewAnimation(transitionContext)
            return
        }
        //消失动画
        DismissViewAnimation(transitionContext)
    }
    
    //展现动画
  private  func presentViewAnimation(transitionContext: UIViewControllerContextTransitioning){
        
        let ToViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
        
        ToViewController.view.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        
        ToViewController.view.transform  = CGAffineTransformMakeScale(1, 0)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            
            ToViewController.view.transform = CGAffineTransformIdentity
            
        }) { (_) in
            
            transitionContext.completeTransition(true)
        }
    }
    //消失动画
  private  func DismissViewAnimation(transitionContext: UIViewControllerContextTransitioning){
        
        let  MenuController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            
            MenuController.view.transform  = CGAffineTransformMakeScale(1, 0.0001)
            
        }) { (_) in
            
            transitionContext.completeTransition(true)
        }
    }



    
}
